package panels;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import classes.FloatRange;

public class FloatRangePanel  extends JTextComponent implements PropertyChangeListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private LayoutManager	layout = null;
	private JDoubleBox		startBox = null;
	private JDoubleBox		sizeBox = null;
	private JLabel			fieldLabel = null;
	private JPanel			rangePanel = null;
	
	public static final String	StartChangedProperty	= "StartChangedProperty";
	public static final String	EndChangedProperty		= "EndChangedProperty";
	
	public FloatRangePanel(String label) {
		layout = new BorderLayout();
		this.setLayout(layout);
	
		rangePanel = new JPanel();
		rangePanel.setLayout(new GridLayout(1,2));
		
		startBox = new JDoubleBox();
		startBox.setFieldName("Start");
		startBox.setVisible(true);
		startBox.addPropertyChangeListener(this);
		startBox.setMinValue(Double.NEGATIVE_INFINITY);
		startBox.setMaxValue(Double.POSITIVE_INFINITY);
		startBox.setValue(0d);
		
		sizeBox = new JDoubleBox();
		sizeBox.setFieldName("Size");
		sizeBox.setVisible(true);
		sizeBox.addPropertyChangeListener(this);
		sizeBox.setMinValue(0d);
		sizeBox.setMaxValue(Double.POSITIVE_INFINITY);
		sizeBox.setValue(0d);
		
		rangePanel.add(startBox);
		rangePanel.add(sizeBox);
		
		fieldLabel = new JLabel("");
		fieldLabel.setHorizontalAlignment(JTextField.CENTER);
		
		setFieldLabel(label);
		
		this.add(fieldLabel, BorderLayout.NORTH);
		this.add(rangePanel, BorderLayout.CENTER);
		
		
		this.setVisible(true);
		
	}

	public String getFieldName() {
		return fieldLabel.getText();
	}
	
	public void setFieldLabel(String text) {
		if (text==null) text = "";
		fieldLabel.setText(text);
		fieldLabel.setVisible(text.length() > 0);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (evt.getSource().equals(startBox)) {
			this.firePropertyChange(StartChangedProperty, evt.getOldValue(), evt.getNewValue());
		} else if (evt.getSource().equals(sizeBox)) {
			this.firePropertyChange(EndChangedProperty, evt.getOldValue(), evt.getNewValue());
		}
		
	}
	
	public double getRangeStart() {
		return startBox.getValue();
	}
	
	public double getRangeSize() {
		return sizeBox.getValue();
	}
	
	public void setRangeStart(double value) {
		startBox.setValue(value);
	}
	
	public void setRangeSize(double value) {
		sizeBox.setValue(value);
	}
	
	public FloatRange	getFloatRange() {
		FloatRange	R = new FloatRange();
		R.set((float) getRangeStart(), (float) (getRangeSize() + getRangeStart()));
		return R;
	}
	
	public double getMinRangeStart() {
		return startBox.getMinValue();
	}
	
	public double getMaxRangeStart() {
		return startBox.getMaxValue();
	}
	
	public void setMinRangeStart(double value) {
		startBox.setMinValue(value);
	}
	
	public void setMaxRangeStart(double value) {
		startBox.setMaxValue(value);
	}
	
	public double getMinRangeSize() {
		return sizeBox.getMinValue();
	}
	
	public double getMaxRangeSize() {
		return sizeBox.getMaxValue();
	}
	
	public void setMinRangeSize(double value) {
		// This CANNOT be less than 0!
		sizeBox.setMaxValue(Math.max(0d,value));
	}
	public void setMaxRangeSize(double value) {
		sizeBox.setMaxValue(Math.max(getMinRangeSize(), value));
	}
	
	
}
